package com.shouhuobao.take.parcel;

import android.app.Activity;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.iseastar.BaseActivity2;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.shouhuobao.station.R;
import com.shouhuobao.take.model.ParcelBean;

/**
 * Created by joker on 2017/7/13.
 */

public class ParcelConfirmTakeActivity extends BaseActivity2 implements View.OnClickListener {

    private TextView mExpressNumber, mStorageCode;
    private TextView mReceiverPhone, mReceiverName;
    private ParcelBean item;

    @Override
    protected void findViewById() {
        setContentView(R.layout.take_parcel_confirm);
        super.findViewById();
        mExpressNumber = (TextView) findViewById(R.id.express_number_TV);
        mStorageCode = (TextView) findViewById(R.id.parcel_code_TV);
        mReceiverPhone = (TextView) findViewById(R.id.parcel_receiver_phone_TV);
        mReceiverName = (TextView) findViewById(R.id.parcel_receive_rname_TV);
        findViewById(R.id.cancel_BT).setOnClickListener(this);
        findViewById(R.id.confirm_BT).setOnClickListener(this);
        getAppTitle().setCommonTitle("更改状态确认");
        item = (ParcelBean) getIntent().getSerializableExtra("item");
        if (item != null) {
            mExpressNumber.setText(item.getExpressCompanyName() + ": " + item.getExpressNumber());
            mStorageCode.setText(getResources().getString(R.string.take_bag_name_desc) + "：" + item.getStorageCode());
            mReceiverPhone.setText(item.getReceiverPhone());
            mReceiverName.setText("取件人姓名: " + item.getReceiverName());
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //取消操作
            case R.id.cancel_BT:
                finish();
                break;
            //确认已取走操作
            case R.id.confirm_BT:
                showLoadingDialog(null);
                AppHttp.getInstance().takeParcelAway(item.getId(), 1);
                break;
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            // 确认已取走
            case MsgID.take_parcel_away:
                cancelLoadingDialog();
                ReqResult<?> result3 = JSON.parser(msg.obj);
                if (checkLoginStatus(result3)) {
                    showToast(result3.getMessage());
                    //                    Intent intent = new Intent();
                    //                    intent.setClass(this, MainActivity.class);
                    //                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    //                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    //                    startActivity(intent);
                    setResult(Activity.RESULT_OK);
                    finish();
                } else {
                    showToast(result3.getMessage());
                }
                return false;
            default:
                return super.handleMessage(msg);
        }
    }
}
